import api from "../../../../utils/api";
import util from "../../../../utils/util";

Page({
    data: {
        province: '',
        mainActiveIndex: 0,
        activeId: null,
        items: [
            {
                "id": 0,
                "text": "华北地区",
                "children": [
                    {"text": "北京市", "id": "北京市"},
                    {"text": "天津市", "id": "天津市"},
                    {"text": "河北省", "id": "河北省"},
                    {"text": "山西省", "id": "山西省"},
                    {"text": "内蒙古自治区", "id": "内蒙古自治区"}
                ]
            },
            {
                "id": 1,
                "text": "东北地区",
                "children": [
                    {"text": "辽宁省", "id": "辽宁省"},
                    {"text": "吉林省", "id": "吉林省"},
                    {"text": "黑龙江省", "id": "黑龙江省"}
                ]
            },
            {
                "id": 2,
                "text": "华东地区",
                "children": [
                    {"text": "上海市", "id": "上海市"},
                    {"text": "江苏省", "id": "江苏省"},
                    {"text": "浙江省", "id": "浙江省"},
                    {"text": "安徽省", "id": "安徽省"},
                    {"text": "福建省", "id": "福建省"},
                    {"text": "江西省", "id": "江西省"},
                    {"text": "山东省", "id": "山东省"}
                ]
            },
            {
                "id": 3,
                "text": "中南地区",
                "children": [
                    {"text": "河南省", "id": "河南省"},
                    {"text": "湖北省", "id": "湖北省"},
                    {"text": "湖南省", "id": "湖南省"},
                    {"text": "广东省", "id": "广东省"},
                    {"text": "广西壮族自治区", "id": "广西壮族自治区"},
                    {"text": "海南省", "id": "海南省"}
                ]
            },
            {
                "id": 4,
                "text": "西南地区",
                "children": [
                    {"text": "重庆市", "id": "重庆市"},
                    {"text": "四川省", "id": "四川省"},
                    {"text": "贵州省", "id": "贵州省"},
                    {"text": "云南省", "id": "云南省"},
                    {"text": "西藏自治区", "id": "西藏自治区"}
                ]
            },
            {
                "id": 5,
                "text": "西北地区",
                "children": [
                    {"text": "陕西省", "id": "陕西省"},
                    {"text": "甘肃省", "id": "甘肃省"},
                    {"text": "青海省", "id": "青海省"},
                    {"text": "宁夏回族自治区", "id": "宁夏回族自治区"},
                    {"text": "新疆维吾尔自治区", "id": "新疆维吾尔自治区"}
                ]
            },
            {
                "id": 6,
                "text": "港澳台地区",
                "children": [
                    {"text": "台湾省", "id": "台湾省"},
                    {"text": "香港特别行政区", "id": "香港特别行政区"},
                    {"text": "澳门特别行政区", "id": "澳门特别行政区"}
                ]
            }
        ]
    },

    // 点击左侧导航栏时触发
    onClickNav({detail = {}}) {
        this.setData({mainActiveIndex: detail.index || 0});
    },

    // 点击右侧选项时触发
    onClickItem({detail = {}}) {
        const activeId = this.data.activeId === detail.id ? null : detail.id;
        const activeText = this.data.province === detail.text ? null : detail.text;
        this.setData({activeId, province: activeText});
    },

    // 修改省份
    updateProvince: function () {
        let province = this.data.province;

        // 发送请求修改省份
        let param = {province, id: wx.getStorageSync("user").id};
        api.put('user', '/update', param).then(res => {
            util.success('修改成功');
            // 0.5秒后切换到 "我的" 选项卡
            setTimeout(() => util.tab('/pages/user/user'), 500);
        }).catch(err => console.log(err));
    },

    // 加载函数
    onLoad: function (options) {
        let province = options['province'];
        this.data.items.forEach(parent => {
            parent.children.forEach(child => {
                if (child.id === province) {
                    this.setData({'mainActiveIndex': parent.id});
                }
            });
        });
        this.setData({
            'province': options['province'],
            'activeId': options['province']
        });
    }
});